<?php
ArrowController::importModel("org.arrowplatform.controls.presentation.table.columns.ArrowAbstractTableColumn");
class ArrowTableTreeColumn extends ArrowAbstractTableColumn{
	
	public function configure(){
		parent::configure();
		$this->setContentCodeKept(true);
	}

	public function init(){
		parent::init();
		if(empty($this->class)) 
			$this->class = 'class="ctrl-table-cell-tree"';
		else
			$this->class = trim($this->class, "\"")." ctrl-table-cell-tree\"";
			
		$table = $this->getParent("ArrowTable");
		$table->setProperty("sortable", 0);
		$data = array( "tableId", $table->getGlobalId(), "model" => $table->getProperty("model") );
		$this->registerJs( get_class($this), dirname(__FILE__)."/resources/".get_class($this).".js", ".ctrl-table-cell-tree", $data );
	}
	
	public function generateRowCell( $context ){
		$width = $this->getProperty("width");
		$field = $this->getProperty("field");
		
		if(strpos($field, ";") !== false){
				$ex = explode(";", $field);
				$val = "";
				foreach( $ex as $_field){
					$val.= $context[$_field]." ";
				}
		}else{
			$val = $context[$field];
		}
		if($mapping = $this->getProperty("mapping"))
			if(isset($mapping[$val]))
				$val = $mapping[$val];
		if($width != -1) {
			$style = 'style="padding-left: '. (15*$context["depth"]) . 'px"';
			
			$path = $context->getPath(false, "_")."_".$context["id"];
			
			if($context->hasChildren())
				$pre = "<a href=\"$path\" rel=\"{$context["depth"]}\" class=\"ctrl-table-field-tree-opened\">&nbsp;</a> ";	
			else	
				$pre = "<a href=\"\" class=\"ctrl-table-field-tree-empty\">&nbsp;</a> ";
			
			$post = '<a href="index.php?arrowDataRequest=8&action=move&model='.$context->getModel().'&direction=up&key='.$context->getPKey().'" title="Przenieś wyżej" class="ctrl-table-move ctrl-table-push-up">&nbsp;</a>';
			$post.= '<a href="index.php?arrowDataRequest=8&action=move&model='.$context->getModel().'&direction=down&key='.$context->getPKey().'" title="Przenieś wyżej" class="ctrl-table-move ctrl-table-push-down">&nbsp;</a>';
				
				
			
			return  "<td path=\"$path\" depth=\"{$context["depth"]}\" {$this->class} $style  >".$pre.$this->getImage($context)."<span>".$val."</span>".$post."<div class=\"id\">{$context["id"]}</div></td>";
		}
		return "";
	}

}

?>